package com.dp.service.impl3;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.JsonPage;
import com.dp.pub.SysProperty;
import com.dp.util.BookConfig;
import com.dp.util.DataMap;
import com.dp.vo.TChannels;
import com.dp.vo.TTreasure;

@Service
@Transactional
public class TreasureServiceImpl extends BaseImpl {
	
	/***
	 * 奖品记录,List
	 * @param hashMap
	 * @param firstrow
	 * @param maxrow
	 * @return
	 */
	public List<TTreasure> findAllTreasureEntity(DataMap hashMap, Integer firstrow,
			Integer maxrow){
		JsonPage jpage=(JsonPage) hashMap.get("jpage");
		TTreasure treasure=(TTreasure) hashMap.get("treasure");
		StringBuffer sqlS = new StringBuffer("SELECT t FROM TTreasure t WHERE 1=1 ");
		if(treasure!=null){
			if(treasure.getStatus()!=null&& treasure.getStatus().toString().trim().length() > 0){
				sqlS.append(" AND t.status like '%"
						+ treasure.getStatus().toString().trim() + "%'");
			}
			if(treasure.getName()!=null&& treasure.getName().toString().trim().length() > 0){
				sqlS.append(" AND t.name like '%"
						+ treasure.getName().toString().trim() + "%'");
			}
			if(treasure.getCode()!=null&& treasure.getCode().toString().trim().length() > 0){
				sqlS.append(" AND t.code like '%"
						+ treasure.getCode().toString().trim() + "%'");
			}
			if(treasure.getWinsn()!=null&& treasure.getWinsn().toString().trim().length() > 0){
				sqlS.append(" AND t.winsn like '%"
						+ treasure.getWinsn().toString().trim() + "%'");
			}
			if(treasure.getWinnumber()!=null&& treasure.getWinnumber().toString().trim().length() > 0){
				sqlS.append(" AND t.winnumber like '%"
						+ treasure.getWinnumber().toString().trim() + "%'");
			}
		}
		
		if(jpage!=null){
			if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
				sqlS.append(" order by t."+jpage.getSidx()+" "+jpage.getSord());
			}
		}
		List<TTreasure> queryList=new ArrayList<TTreasure>();
		try {
			Query query = em.createQuery(sqlS.toString());
			if (firstrow != null) {
				query.setFirstResult(firstrow);
			}
			if (maxrow != null) {
				query.setMaxResults(maxrow);
			}
			queryList = query.getResultList();
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return queryList;
	}
	
	/**
	 * 奖品记录数
	 * @param hashMap
	 * @return
	 */
	public Integer findAllTreasureEntityCount(DataMap hashMap){
		TTreasure treasure=(TTreasure) hashMap.get("treasure");
		Integer iCount = 0; 
		StringBuffer sqlS = new StringBuffer("SELECT count(t.id) FROM TTreasure t WHERE 1=1 ");
		if(treasure!=null){
			if(treasure.getStatus()!=null&& treasure.getStatus().toString().trim().length() > 0){
				sqlS.append(" AND t.status like '%"
						+ treasure.getStatus().toString().trim() + "%'");
			}
			if(treasure.getName()!=null&& treasure.getName().toString().trim().length() > 0){
				sqlS.append(" AND t.name like '%"
						+ treasure.getName().toString().trim() + "%'");
			}
			if(treasure.getCode()!=null&& treasure.getCode().toString().trim().length() > 0){
				sqlS.append(" AND t.code like '%"
						+ treasure.getCode().toString().trim() + "%'");
			}
			if(treasure.getWinsn()!=null&& treasure.getWinsn().toString().trim().length() > 0){
				sqlS.append(" AND t.winsn like '%"
						+ treasure.getWinsn().toString().trim() + "%'");
			}
			if(treasure.getWinnumber()!=null&& treasure.getWinnumber().toString().trim().length() > 0){
				sqlS.append(" AND t.winnumber like '%"
						+ treasure.getWinnumber().toString().trim() + "%'");
			}
		}
		
		Query query=null;
		try {
			query = em.createQuery(sqlS.toString());
			iCount = Integer.valueOf(query.getSingleResult().toString());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return iCount;
	}

	/**
	 * 系统参数“hdnews_rootch”下所有栏目
	 * @return
	 */
	public List<TChannels> findHdnewsChannelL() {
		StringBuffer sqlS = new StringBuffer("SELECT t FROM TChannels t WHERE 1=1 ");
		List<TChannels> queryList=new ArrayList<TChannels>();
		String hdnews_rootch = SysProperty.getPrep("hdnews_rootch");
        if(hdnews_rootch == null || "".equals(hdnews_rootch)){
        	return queryList;
        }
        sqlS.append(" AND channelType='"+BookConfig.channel_type_hdnews+"' and channelClassifyingLabel is not null" +
        		" AND previousId='"+hdnews_rootch+"'");
		try {
			Query query = em.createQuery(sqlS.toString());
			queryList = query.getResultList();
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return queryList;
	}

	/**
	 * 添加
	 * @param treasure
	 * @return
	 */
	public Boolean addTreasure(TTreasure treasure) {
		Boolean resultb=true;
		try {
			em.persist(treasure);
		} catch (Exception e) {
			logger.error("addTreasure fail !!!" + e.getMessage());
			e.printStackTrace();
			resultb=false;
		} 
		return resultb;
	}

	/**
	 * 根据id得到奖品对象
	 * @param id
	 * @return
	 */
	public TTreasure findTreasureByid(Integer id) {
		TTreasure o = null;
		try {
			o = this.em.find(TTreasure.class, id);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return o;
	}

	/**
	 * 修改
	 * @param treasure
	 * @return
	 */
	public boolean updateTreasure(TTreasure treasure) {
		Boolean resultb=true;
//		try {
//			this.init();
//			this.sql="update t_treasure set name=?,info=?,receiveway=?,receivenumber=?,overduetime=?,position=?,status=?,channel_id=? where id=?";
//			this.prep=this.conn.prepareStatement(sql);
//			prep.setString(1,treasure.getName());
//			prep.setString(2,treasure.getInfo());
//			prep.setString(3,treasure.getReceiveway());
//			prep.setString(4,treasure.getReceivenumber());
//			prep.setString(5,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(treasure.getOverduetime()));
//			prep.setInt(6,treasure.getPosition());
//			prep.setInt(7,1);
//			prep.setString(8,treasure.getChannelId());
//			prep.setInt(9,treasure.getId());
//			prep.executeUpdate();
//			conn.commit(); 
//		} catch (Exception e) {
//			logger.info("updateTreasure fail !!!" + e.getMessage());
//			e.printStackTrace();
//			resultb=false;
//			if(conn!=null){
//				try {
//					conn.rollback();
//				} catch (SQLException e1) {
//					e1.printStackTrace();
//				} 
//			}
//		} finally {
//			this.closeAll();
//		}
		return resultb;
	}

	public boolean updateTreasureStatus(String strIdList, Integer status) {
		try {
			// 更新奖品状态
			String jpql = "UPDATE TTreasure SET status='" + status
					+ "' WHERE id in (" + strIdList + ")";
			Query query = em.createQuery(jpql);
			Object obj = query.executeUpdate();
		} catch (Exception e) {
			return false;
		} 
		return true;
	}
	
}
